<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<%@ attribute name="recordId" required="true" rtexprvalue="true"%>
<%@ attribute name="moduleCode" required="true" rtexprvalue="true"%>

<div id="tagPanel" ng-controller="TagCtrl">
	<div class="input-append">
		<input type="text" class="span2" id="tagName" />
		<button type="button" class="btn btn-primary" ng-click="addTags();">
			<i class="icon-plus"></i>
			<s:message code="btn.add" />
		</button>
		</p>
	</div>

	<div>
		<ul class="inline"  >
			<li style="color: #3a87ad"" ng-repeat="row in tags" id="tag{{$index}}" ng-mouseover="showflag=1" ng-mouseleave="showflag=0" ng-init="showflag=0">
			{{row.tag.name}}
			<span class="pull-right" ng-show="showflag"> <a  ng-click=deleteTagAssoc($index) target="_blank" title="删除"><i class="icon-remove"></i></a>
			</span>
			</li>
		</ul>
	</div>
</div>

<script type="text/javascript" src="${contextPath }/theme/default/js/angular.js"></script>
<script type="text/javascript">
	$(document).ready(function() {

		angular.element(document).ready(function() {
			angular.bootstrap(document, [ function($httpProvider) {

				// Use x-www-form-urlencoded Content-Type
				$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

				/**
				 * The workhorse; converts an object to x-www-form-urlencoded serialization.
				 * @param {Object} obj
				 * @return {String}
				 */
				var param = function(obj) {
					var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

					for (name in obj) {
						value = obj[name];

						if (value instanceof Array) {
							for (i = 0; i < value.length; ++i) {
								subValue = value[i];
								fullSubName = name + '[' + i + ']';
								innerObj = {};
								innerObj[fullSubName] = subValue;
								query += param(innerObj) + '&';
							}
						} else if (value instanceof Object) {
							for (subName in value) {
								subValue = value[subName];
								fullSubName = name + '[' + subName + ']';
								innerObj = {};
								innerObj[fullSubName] = subValue;
								query += param(innerObj) + '&';
							}
						} else if (value !== undefined && value !== null)
							query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
					}

					return query.length ? query.substr(0, query.length - 1) : query;
				};

				// Override $http service's default transformRequest
				$httpProvider.defaults.transformRequest = [ function(data) {
					return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
				} ];
			} ]);
		});
	});

	function TagCtrl($scope, $http) {
		$scope.tags = [];
		$scope.formData = {};
		//init
		$http.get('${contextPath}/assistant/tag/getTags.json?moduleCode=${moduleCode}&recordId=${recordId}', {}).success(function(data) {
			$scope.tags = data.tags;
		});

		$scope.addTags = function() {
			var tagName = $("#tagName").val();
			$http.get('${contextPath}/assistant/tag/addTag.json?moduleCode=${moduleCode}&recordId=${recordId}&name=' + tagName, {}).success(function() {
				$("#tagName").empty();
				$scope.reloadTags();
			});
		}
		
		$scope.reloadTags = function() {
			$http.get('${contextPath}/assistant/tag/getTags.json?moduleCode=${moduleCode}&recordId=${recordId}', {}).success(function(data) {
				$scope.tags = data.tags;
			});
		}
		$scope.deleteTagAssoc=function(index){
			var tag=$scope.tags[index];
			  $http.get('${contextPath }/assistant/tag/deleteTagAssoc.do?assocId='+tag.assoc.id, {
			  }).success(function(){
				  $scope.tags.splice(index,1);
			  } );
		}
	}
</script>
